package com.ymatou.iapi.login.testcase;

import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.login.parameter.*;
import com.ymatou.iapi.login.service.*;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.Now;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P2;
import com.ymttest.business.data.LoginSafeConfiguration;
import com.ymttest.business.service.AuthCallService;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.database.model.LoginLog;
import com.ymttest.database.mongo.MongoLoginHistoryWapper;
import com.ymttest.database.mongo.ULC201406Bean;
import com.ymttest.database.mongo.ULC201406Wapper;
import com.ymttest.database.mongo.YmatouBlacklistUserWapper;
import com.ymttest.database.sqlwapper.IPWapper;
import com.ymttest.database.sqlwapper.JUserWapper;
import com.ymttest.database.sqlwapper.LoginHistoryWapper;
import com.ymttest.database.sqlwapper.LoginLogWapper;
import com.ymttest.database.sqlwapper.UserSafeWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.constant.ECode;

/**
*APP端设备认证接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetSellerLoginLogs{
	private static GetSellerLoginLogsBean GetSellerLoginLogsBean;
	private static GetSellerLoginLogsCall GetSellerLoginLogsCall;
	private LoginLogWapper loginLogWapper;
	
	@BeforeClass
	public static void setUp() throws Exception {
		Logger.createResultFile("查询买手登入日志接口");
	}

	@Before
	public void caseUp() {
		GetSellerLoginLogsBean=new GetSellerLoginLogsBean();
		GetSellerLoginLogsCall=new GetSellerLoginLogsCall();
		loginLogWapper=new LoginLogWapper();
	}

	@AfterClass
	public static void tearDown() throws Throwable {
		Logger.generateResult(GetSellerLoginLogsCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetSellerLoginLogs_001() {
		Logger.start(true,"查询买手登入日志,成功");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			regbean.setRegisterType(1);  
			regbean.setClientIp("120.1.1.1");
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
//			JsonObject result = urrc.getJsonObject().get("Result").getAsJsonObject();
//			String token = result.get("Token").getAsString();
			int uid = urrc.getUserid();
			Thread.sleep(1000);
			JUserCallService.login(regbean.getLoginId(), regbean.getPas(), "sellerapp", regbean.getClientIp(), regbean.getDeviceId());
			
			GetSellerLoginLogsBean.setSellerId(uid);
			GetSellerLoginLogsCall.setData(GetSellerLoginLogsBean);
			GetSellerLoginLogsCall.callService();
			
			LoginLogInfos[] loginInfos=GetSellerLoginLogsCall.getResult().getloginLogInfos();
			List<LoginLog> logs=loginLogWapper.selectByuserid(uid);
			
			Logger.verifyEquals("0", GetSellerLoginLogsCall.getString("ResponseCode"), "验证code");
			Logger.verifyEquals("true", GetSellerLoginLogsCall.getString("Success"), "验证是否成功");
			Logger.verifyEquals("0", GetSellerLoginLogsCall.getString("ErrorCode"), "验证ErrorCode是否成功");
			if(loginInfos.length>0){
				for(int i=0;i<loginInfos.length;i++)
				{
					LoginLog log=logs.get(i);
					Logger.verifyEquals(log.getCreateTime(), loginInfos[i].getcreateTime(), "验证loginLogInfos集合第"+(int)(i+1)+"个"+"对象的createTime是否正确");
					Logger.verifyEquals(GetCountry(log.getClientIp()), loginInfos[i].getcountry(), "验证loginLogInfos集合第"+(int)(i+1)+"个"+"对象的country是否正确");
					Logger.verifyEquals(GetProvince(log.getClientIp()), loginInfos[i].getprovince(), "验证loginLogInfos集合第"+(int)(i+1)+"个"+"对象的province是否正确");
					Logger.verifyEquals(GetCity(log.getClientIp()), loginInfos[i].getcity(), "验证loginLogInfos集合第"+(int)(i+1)+"个"+"对象的city是否正确");
					Logger.verifyEquals(log.getClientIp(), loginInfos[i].getclientIp(), "验证loginLogInfos集合第"+(int)(i+1)+"个"+"对象的clientIp是否正确");
					Logger.verifyEquals(log.getTerminalSource().toString(), loginInfos[i].getterminalSource(), "验证loginLogInfos集合第"+(int)(i+1)+"个"+"对象的terminalSource是否正确");
					Logger.verifyEquals(log.getResponseCode().toString(), loginInfos[i].getstatusCode(), "验证loginLogInfos集合第"+(int)(i+1)+"个"+"对象的statusCode是否正确");
				}
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetSellerLoginLogs_002() {
		Logger.start(true,"查询买家登入日志,成功");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			regbean.setRegisterType(0);  
			regbean.setClientIp("120.1.1.2");
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			JsonObject result = urrc.getJsonObject().get("Result").getAsJsonObject();
			int uid = urrc.getUserid();
			Thread.sleep(3000);
            JUserCallService.login(regbean.getLoginId(), regbean.getPas(), "sellerapp", regbean.getClientIp(), regbean.getDeviceId());
			GetSellerLoginLogsBean.setSellerId(uid);
			GetSellerLoginLogsCall.setData(GetSellerLoginLogsBean);
			GetSellerLoginLogsCall.callService();
			
			LoginLogInfos[] loginInfos=GetSellerLoginLogsCall.getResult().getloginLogInfos();
			List<LoginLog> logs=loginLogWapper.selectByuserid(uid);
			
			Logger.verifyEquals("0", GetSellerLoginLogsCall.getString("ResponseCode"), "验证code");
			Logger.verifyEquals("true", GetSellerLoginLogsCall.getString("Success"), "验证是否成功");
			Logger.verifyEquals("0", GetSellerLoginLogsCall.getString("ErrorCode"), "验证ErrorCode是否成功");
			if(loginInfos.length>0){
				for(int i=0;i<loginInfos.length;i++)
				{
					LoginLog log=logs.get(i);
					Logger.verifyEquals(log.getCreateTime(), loginInfos[i].getcreateTime(), "验证loginLogInfos集合第"+(int)(i+1)+"个"+"对象的createTime是否正确");
					Logger.verifyEquals(GetCountry(log.getClientIp()), loginInfos[i].getcountry(), "验证loginLogInfos集合第"+(int)(i+1)+"个"+"对象的country是否正确");
					Logger.verifyEquals(GetProvince(log.getClientIp()), loginInfos[i].getprovince(), "验证loginLogInfos集合第"+(int)(i+1)+"个"+"对象的province是否正确");
					Logger.verifyEquals(GetCity(log.getClientIp()), loginInfos[i].getcity(), "验证loginLogInfos集合第"+(int)(i+1)+"个"+"对象的city是否正确");
					Logger.verifyEquals(log.getClientIp(), loginInfos[i].getclientIp(), "验证loginLogInfos集合第"+(int)(i+1)+"个"+"对象的clientIp是否正确");
					Logger.verifyEquals(log.getTerminalSource().toString(), loginInfos[i].getterminalSource(), "验证loginLogInfos集合第"+(int)(i+1)+"个"+"对象的terminalSource是否正确");
					Logger.verifyEquals(log.getResponseCode().toString(), loginInfos[i].getstatusCode(), "验证loginLogInfos集合第"+(int)(i+1)+"个"+"对象的statusCode是否正确");
				}
			}
		} catch (Exception e) {
			Logger.fail(e);
	 }
	}
	
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetSellerLoginLogs_003() {
		Logger.start(false,"查询不存在的卖家登入日志,查询为空");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			regbean.setRegisterType(0);  
			regbean.setClientIp("120.1.1.3");
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			JsonObject result = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = result.get("Token").getAsString();
			//int uid = urrc.getUserid();
			int uid = 999999;
			
            JUserCallService.login(regbean.getLoginId(), regbean.getPas(), "sellerapp", regbean.getClientIp(), regbean.getDeviceId());
			GetSellerLoginLogsBean.setSellerId(uid);
			GetSellerLoginLogsCall.setData(GetSellerLoginLogsBean);
			GetSellerLoginLogsCall.callService();
			LoginLogInfos[] loginInfos=GetSellerLoginLogsCall.getResult().getloginLogInfos();
			List<LoginLog> logs=loginLogWapper.selectByuserid(uid);
			
			Logger.verifyEquals("0", GetSellerLoginLogsCall.getString("ResponseCode"), "验证code");
			Logger.verifyEquals("true", GetSellerLoginLogsCall.getString("Success"), "验证是否成功");
			Logger.verifyEquals("0", GetSellerLoginLogsCall.getString("ErrorCode"), "验证ErrorCode是否成功");
			if(loginInfos.length>0){
				for(int i=0;i<loginInfos.length;i++)
				{
					LoginLog log=logs.get(i);
					Logger.verifyEquals(log.getCreateTime(), loginInfos[i].getcreateTime(), "验证loginLogInfos集合第"+(int)(i+1)+"个"+"对象的createTime是否正确");
					Logger.verifyEquals(GetCountry(log.getClientIp()), loginInfos[i].getcountry(), "验证loginLogInfos集合第"+(int)(i+1)+"个"+"对象的country是否正确");
					Logger.verifyEquals(GetProvince(log.getClientIp()), loginInfos[i].getprovince(), "验证loginLogInfos集合第"+(int)(i+1)+"个"+"对象的province是否正确");
					Logger.verifyEquals(GetCity(log.getClientIp()), loginInfos[i].getcity(), "验证loginLogInfos集合第"+(int)(i+1)+"个"+"对象的city是否正确");
					Logger.verifyEquals(log.getClientIp(), loginInfos[i].getclientIp(), "验证loginLogInfos集合第"+(int)(i+1)+"个"+"对象的clientIp是否正确");
					Logger.verifyEquals(log.getTerminalSource().toString(), loginInfos[i].getterminalSource(), "验证loginLogInfos集合第"+(int)(i+1)+"个"+"对象的terminalSource是否正确");
					Logger.verifyEquals(log.getResponseCode().toString(), loginInfos[i].getstatusCode(), "验证loginLogInfos集合第"+(int)(i+1)+"个"+"对象的statusCode是否正确");
				}
			}
		} catch (Exception e) {
			Logger.fail(e);
	 }
	}
	private String GetProvince(String ip)
	{
		return new IPWapper().selectIpInfo(TestDataManager.converIpToLong(ip)).get(0).getProvince();
	}
	private String GetCity(String ip)
	{
		return new IPWapper().selectIpInfo(TestDataManager.converIpToLong(ip)).get(0).getCity();
	}
	private String GetCountry(String ip)
	{
		return new IPWapper().selectIpInfo(TestDataManager.converIpToLong(ip)).get(0).getCountry();
	}
	
}